/*******************************************************************************

                                MAPUTIL.C

  Title:        Utilities to read and write map files.
  Author:       Hanna Szoke
  Date:         May 7, 2003
  Function:     This module deals with the I/O of .map files and their
		interconversions with respect to .bin files.

*******************************************************************************/

#include "util.h"
#include "cellparams.h"
#include "dims.h"
#include "symmetry.h"

#define	TINY	1.e-10		/* for round-off of output */

static	int     Npout ;                 /* product of Nxout*Nyout*Nzout */
static	int	headread ;		/* # lines of header, as read:
					7 for MAPMAN, 8 for O and SPEDEN */


#include	<sys/time.h>	/* ... for picking up date & time	*/

	/************************************************************
	Write a file of output electron densities in XPLOR format.
	************************************************************/

void	printXmap(char *out_filename, char Range[], 
                  int Nxout, int Nyout, int Nzout, real *array)	
{
     FILE 	*fp ;
     int	get_min_max(real *, int, real *, real *) ;
     int	z, n, i, j ;
     real	ar_min, ar_max ;
     char	xname[MAXSTRING] ;

     strcpy(xname, out_filename) ;
     strcat(xname, ".xplor") ;
     if ((fp = fopen(xname, "w")) == NULL)
          Fatal("Couldn't open .xplor file for writing.") ;

     /* Ensure that no tiny values appear */

     Npout = Nxout*Nyout*Nzout ;

     for (n = 0; n < Npout; n++) 
    	  if (fabs(*(array+n)) < TINY)
	       *(array+n) = 0 ;

     get_min_max(array, Npout, &ar_min, &ar_max) ;

     if (ar_min == ar_max) 
	  Fatal("All the data are effectively equal!") ;

     fprintf(fp,"\n       3 !NTITLE\n") ; 
     fprintf(fp," REMARKS Electron Density Information from SPEDEN\n") ;
     fprintf(fp," REMARKS DATE: %s \n", timestamp()) ;
     fprintf(fp," REMARKS Density range is (%g, %g)\n", ar_min, ar_max) ;
     fprintf(fp,"%s\n", Range) ;
     fprintf(fp,"%12.5E%12.5E%12.5E%12.5E%12.5E%12.5E\n",
	     aAxis, bAxis, cAxis, angles[0], angles[1], angles[2]) ;
     fprintf(fp, "ZYX\n") ;

     z = 0 ;
     for (n = 0; n < Nzout; n++, z++) {
        fprintf(fp, "%8d\n", z) ;
        for (i = 0; i < Nxout*Nyout; i += 6) {
         for (j = 0; j < 6; j++)
	   if (i+j < Nxout*Nyout)
                fprintf(fp, "%12.5E", *(array+n*Nxout*Nyout+i+j)) ;
	 fprintf(fp, "\n") ;
        }
     }
     fprintf(fp, "   -9999\n") ;
     fclose(fp) ;
}


        /************************************************************
        Read header of file of electron densities in XPLOR format.
        ************************************************************/

void    readXmap_header(char *in_filename, int *Nx, int *Ny, int *Nz)
{
     int        first3chars() ;
     FILE       *fp ;
     int	Nxmap, Nymap, Nzmap ;
     int	rlimx[2], rlimy[2], rlimz[2] ;	/* coord limits */
     char	*ext = ".map" ;

     if (strstr(in_filename, ext) == NULL)
	  strcat(in_filename, ext) ;

     check_exist(in_filename) ;
     fp = fopen(in_filename, "r") ;

     /************************************************************
     			Read header stuff  
     ************************************************************/

     headread = 0 ;

     do {
          fgets(nextline, MAXSTRING, fp) ;
          fprintf(fp_log, "%s", nextline) ;
          headread++ ;
     } while (first3chars(nextline, "REMARKS") != 0) ;

     do {
          fgets(nextline, MAXSTRING, fp) ;
          fprintf(fp_log, "%s", nextline) ;
          headread++ ;
     } while (first3chars(nextline, "REMARKS") == 0) ;

     sscanf(nextline, "%8d %8d %8d %8d %8d %8d %8d %8d %8d", 
                      &Nxmap, &rlimx[0], &rlimx[1], 
                      &Nymap, &rlimy[0], &rlimy[1], 
                      &Nzmap, &rlimz[0], &rlimz[1]) ;
     *Nx = Nxmap ;
     *Ny = Nymap ;
     *Nz = Nzmap ;

     fgets(nextline, MAXSTRING, fp) ;	/* cell dimensions */
     headread++ ;
     fprintf(fp_log, "%s", nextline) ;
     fgets(nextline, MAXSTRING, fp) ;	/* ordering */
     headread++ ;
     fprintf(fp_log, "%s", nextline) ;
     fprintf(fp_log, "\n") ;

     fclose(fp);
     return ;
}

        /************************************************************
        Read data in file of electron densities in XPLOR format.
        ************************************************************/

void    readXmap(char *in_filename, int Nx, int Ny, int Nz, real *array)
{
     FILE       *fp ;
     int        n, i ;
     int	nval ;
     real     *part ;   

     check_exist(in_filename) ;

     fp = fopen(in_filename, "r") ;

     /************************************************************
     			Skip header stuff  
     ************************************************************/

     for  (n = 0; n < headread; n++) 
          fgets(nextline, MAXSTRING, fp) ;

     /************************************************************
     			Now read data	
     ************************************************************/

     for (n = 0; n < Nz; n++) {

        fgets(nextline, MAXSTRING, fp) ;
        part = array + n*Nx*Ny ;

        for (i = 0; i < Nx*Ny; i += 6) {
             fgets(nextline, MAXSTRING, fp) ;
             nval = sscanf(nextline, "%g %g %g %g %g %g",
                    &t0, &t1, &t2, &t3, &t4, &t5) ;
                    *(part+i+0) = t0 ;
                    *(part+i+1) = t1 ;
                    *(part+i+2) = t2 ;
                    *(part+i+3) = t3 ;
                    *(part+i+4) = t4 ;
                    *(part+i+5) = t5 ;
        }
     }

     fclose(fp);
     return ;
}
